package com.community.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.community.entity.User;
import com.community.service.IUserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private IUserService iUserService;
	
	@RequestMapping("/detail")
	public String userDetail(HttpSession session,ModelMap modelMap){
		if(session.getAttribute("userid") == null){
			return "/user/login";
		}else{
			Long id = Long.valueOf(session.getAttribute("userid").toString());
			User u = iUserService.getUser(id);
			modelMap.addAttribute("user", u);
			return "/user/user_detail";
		}
	}
	
	@RequestMapping("/forget")
	public String forgerPassword(){
		return "/user/forget_password";
	}
	
	@RequestMapping("/update/password")
	@ResponseBody
	public int updatePassword(String phone,String code,String pwd){
		if(code.equals("123456")){
			User u = iUserService.getUserByCount(phone);
			if(u == null){
				return 0;
			}else{
				u.setPassword(pwd);
				iUserService.updateUser(u);
				return 1;
			}
		}else{
			return 2;
		}
		
		
	
		
	}
	
}
